// pages/doctor/doctorList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    doctorList: [],
    page: 1,
    pageSize: 10,
    list: [],
    isBottom: false,
    isLoad: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let page = this.data.page;
    let pagesize = this.data.pageSize;
    this.getDoctorListByPage(page, pagesize);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoad: true,
    })
    if (this.data.isBottom) {
      setTimeout(() => {
        this.setData({
          isLoad: false,
        })
      }, 2000)
    } else {
      this.data.page++;
      let size = this.data.pageSize;
      this.getDoctorListByPage(this.data.page, size);
      // console.log(this.data.page + ": " + size)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getDoctorListByPage(page, size) {
    wx.showLoading()
    let that = this;
    let urlBase = app.globalData.urlBase;

    wx.request({
      url: urlBase + '/wx/getDoctors',
      method: 'get',
      data: {
        page: page,
        pagesize: size
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data.length === 0) {
          that.setData({
            isBottom: true
          });
        } else {
          that.data.list = that.data.list.concat(res.data);
          that.setData({
            doctorList: that.data.list
          });
        }
        that.setData({
          isLoad: false,
        })
        // console.log(res.data)
        wx.hideLoading()
      }
    })
  }
})